/// <reference types="cypress" />
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//
// declare global {
//   namespace Cypress {
//     interface Chainable {
//       login(email: string, password: string): Chainable<void>
//       drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
//       dismiss(subject?: string, options?: Partial<TypeOptions>): Chainable<Element>
//       visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
//     }
//   }
// }

// 自定义命令：添加TODO项目
Cypress.Commands.add('addTodo', (text: string) => {
  cy.get('[data-cy="todo-input"]').type(text)
  cy.get('[data-cy="add-button"]').click()
})

// 自定义命令：验证TODO项目存在
Cypress.Commands.add('shouldHaveTodo', (index: number, text: string) => {
  cy.get(`[data-cy="todo-item-${index}"]`).should('contain', text)
})

// 自定义命令：删除TODO项目
Cypress.Commands.add('deleteTodo', (index: number) => {
  cy.get(`[data-cy="delete-button-${index}"]`).click()
})

// 自定义命令：切换TODO完成状态
Cypress.Commands.add('toggleTodo', (index: number) => {
  cy.get(`[data-cy="todo-checkbox-${index}"]`).click()
})

// 自定义命令：编辑TODO项目
Cypress.Commands.add('editTodo', (index: number, newText: string) => {
  cy.get(`[data-cy="todo-text-${index}"]`).dblclick()
  cy.get(`[data-cy="todo-edit-input-${index}"]`).clear().type(newText).type('{enter}')
})

declare global {
  namespace Cypress {
    interface Chainable {
      addTodo(text: string): Chainable<void>
      shouldHaveTodo(index: number, text: string): Chainable<void>
      deleteTodo(index: number): Chainable<void>
      toggleTodo(index: number): Chainable<void>
      editTodo(index: number, newText: string): Chainable<void>
    }
  }
}
